package com.elitel.frame.web.jdp_common_category.service;


import com.elitel.frame.web.common.idworker.IdWorker;
import com.elitel.frame.web.jdp_common_category.dao.JdpCommonCategoryMapper;
import com.elitel.frame.web.jdp_common_category.entity.JdpCommonCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * JDP通用目录树 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-06-10
 */
@Service
public class JdpCommonCategoryServiceImpl implements IJdpCommonCategoryService {
    @Resource
    JdpCommonCategoryMapper jdpCommonCategoryMapper;

    @Override
    public String insert(JdpCommonCategory record) {
        record.setCategoryId(IdWorker.createStrId());
        record.setCreatedTime(new Date());
        int insert = jdpCommonCategoryMapper.insert(record);
        return insert > 0 ? record.getCategoryId() : "insert failed";
    }

    @Override
    public String update(JdpCommonCategory record) {
        int update = jdpCommonCategoryMapper.update(record);
        return update > 0 ? record.getCategoryId() : "update failed";
    }

    @Override
    public String deleteById(String id) {
        int delete = jdpCommonCategoryMapper.deleteById(id);
        return delete > 0 ? id : "delete failed";
    }

    @Override
    public JdpCommonCategory selectById(String id) {
        return jdpCommonCategoryMapper.selectById(id);
    }

    @Override
    public Map<String, Object> selectAll(String name, Integer page, Integer rows) {
        if (page == null) page = 1;
        if (rows == null) rows = 10;
        PageHelper.startPage(page, rows);
        List<JdpCommonCategory> list = jdpCommonCategoryMapper.selectAll(name);
        PageInfo<JdpCommonCategory> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", pageInfo.getTotal());
        return map;
    }
}
